package com.fzz.learning_forum.controller;

import com.fzz.learning_forum.domain.Admin;
import com.fzz.learning_forum.domain.User;
import com.fzz.learning_forum.service.AdminService;
import com.fzz.learning_forum.service.UserService;
import com.fzz.learning_forum.serviceImpl.AdminServiceImpl;
import com.fzz.learning_forum.serviceImpl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @Autowired
    private UserService userService=new UserServiceImpl();

    @Autowired
    private AdminService adminService=new AdminServiceImpl();

    @RequestMapping(value = "/login")
    public String login(HttpSession session,String username, String password,String type) {
        if (type.equals("用户")) {
            User user = userService.userLogin(username, password);
            if (user != null && user.getValid().equals(1)) {
                session.setAttribute("user", user);
                return "redirect:/user/";
            }
        } else
        {
            Admin admin = adminService.adminLogin(username, password);
            if(admin!=null)
            {
                session.setAttribute("admin",admin);
                return "back/index";
            }
        }
        return "comm/login";
    }

    @RequestMapping(value = "/user/logout")
    public String userLogout(HttpSession session)
    {
        session.invalidate();
        return "comm/login";
    }
}
